package com.kunlong.config.datasource;

import com.alibaba.druid.filter.config.ConfigTools;
import com.alibaba.druid.pool.DruidDataSource;
import com.kunlong.config.fastdfs.FastdfsConfig;
import com.kunlong.consts.CodegenConsts;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.hbatis.spring.HbatisSqlSessionDaoSupport;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;


import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Configuration("codegenDatasourceConfig")
public class CodegenDatasourceConfig {

	@Qualifier("codegenDataSource")
	@Bean(name = "codegenDataSource")
	@ConfigurationProperties(prefix = "spring.datasource.app.druid")
	public DataSource codegenDataSource() {
		DruidDataSource druidDataSource = new DruidDataSource();

		return druidDataSource;
	}
	void decrypt(DruidDataSource druidDataSource) throws Exception {
		druidDataSource.setPassword(ConfigTools.decrypt(druidDataSource.getPassword()));
		druidDataSource.setUsername(ConfigTools.decrypt(druidDataSource.getUsername()));

	}
	public  void decrypt(String text) throws Exception {
		ConfigTools.decrypt(text);

	}
	public  void encrypt(String text) throws Exception {
		ConfigTools.encrypt(text);

	}
	@Bean(name = "codegenSqlSessionFactory")
	@Primary
	public SqlSessionFactory codegenSqlSessionFactory(@Qualifier("codegenDataSource") DataSource dataSource)
			throws Exception {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(dataSource);

		decrypt((DruidDataSource) dataSource);

		PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		List<Resource> resList = new ArrayList<Resource>();

		//String[] ss = mybatisMapperLocations.split(",");
		//for(String location:ss) {
		for (String location : CodegenConsts.MYBATIS_MAPPER_LOCATIONS) {
			resList.addAll(Arrays.asList(resolver.getResources(location)));

		}
		bean.setMapperLocations(resList.toArray(new Resource[0]));
		
		SqlSessionFactory sessionFactory = bean.getObject();
		sessionFactory.getConfiguration().setMapUnderscoreToCamelCase(true);
		HbatisSqlSessionDaoSupport.setHbatisSessionFactory(sessionFactory);
		return sessionFactory;
	}


	@Bean(name = "codegenMapperScannerConfigurer")
	public MapperScannerConfigurer mapperScannerConfigurer() {
		MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
		mapperScannerConfigurer.setSqlSessionFactoryBeanName("codegenSqlSessionFactory");
		mapperScannerConfigurer.setBasePackage(CodegenConsts.MYBATIS_BASE_PACKAGE);
		//mapperScannerConfigurer.setBasePackage(mybatisBasePackage);


		return mapperScannerConfigurer;
	}

	@Bean(name = "codegenTransactionManager")
	@Primary
	public DataSourceTransactionManager codegenTransactionManager(
			@Qualifier("codegenDataSource") DataSource dataSource) {
		return new DataSourceTransactionManager(dataSource);
	}




}